package com.situ.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Coach;
import com.situ.gym.pojo.query.CoachQuery;
import com.situ.gym.service.ICoachService;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/coach")
public class CoachController {
    //注入过来service
    @Autowired
    private ICoachService coachService;

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Coach> list = coachService.list();
        return Result.ok(list);
    }

    //Restful    GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
    @GetMapping("/list")
    public Result list(CoachQuery coachQuery) {
        IPage<Coach> page = coachService.list(coachQuery);
        return Result.ok(page);
    }

    // /coach/deleteById/12
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        coachService.removeById(id);
        return Result.ok("删除成功");
    }

    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        coachService.removeBatchByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Coach coach) {
        coachService.save(coach);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Coach coach = coachService.getById(id);
        return Result.ok(coach);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Coach coach) {
        // update coach set name='zhangsn',age=23 where id=1
        coachService.updateById(coach);
        return Result.ok("修改成功");
    }

}

